package no.hib.quiz;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NewQuizFragment extends Fragment{
	

	private Quiz quiz;
	private int qNr;
	private View aView;
	
	
	public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);

	    quiz = new Quiz();
	    qNr = 1;
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
		aView = inflater.inflate(R.layout.new_quiz, container, false);
        
    
    	
    	Button btnAdd =(Button)aView.findViewById(R.id.btn_add_qanda);
	    btnAdd.setOnClickListener(new OnClickListener() {
	    	
	        @Override
	        public void onClick(View v) { 
	 
	        	buttonMethod();
            
	        }
	    });
		
		
    	return aView;
    }
	
	public void buttonMethod() {
		
		if (quiz == null) {
			quiz = new Quiz();
		}
		
		
		String n = ((EditText) aView.findViewById(R.id.et_quiz_name)).getText().toString();
    	String q = ((EditText) aView.findViewById(R.id.et_question)).getText().toString(); 
    	String a = ((EditText) aView.findViewById(R.id.et_answer)).getText().toString();
    	System.out.println("Start"+n+q+a+"End");

    	if (qNr < 10) {
    		if (!q.equals("") && !a.equals("")) {
        		QuestionAndAnswer qa = new QuestionAndAnswer(q, Integer.parseInt(a));
        		quiz.add(qa);
        		qNr++;
        		TextView tvQuestion = (TextView) aView.findViewById(R.id.tv_enter_question);
        		tvQuestion.setText(("Enter question " + qNr));
        		TextView tvAnswer = (TextView) aView.findViewById(R.id.tv_enter_answer);
        		tvAnswer.setText(("Enter answer " + qNr));
        		EditText etQuestion = (EditText) aView.findViewById(R.id.et_question);
        		etQuestion.setText("");
        		EditText etAnswer = (EditText) aView.findViewById(R.id.et_answer);
        		etAnswer.setText("");
        		
        	}
    		else {
    			EditText etQuestion = (EditText) aView.findViewById(R.id.et_question);
        		etQuestion.setHint("Enter question here");
        		EditText etAnswer = (EditText) aView.findViewById(R.id.et_answer);
        		etAnswer.setHint("Enter answer here");
    		}
    	}
    	else {
    		if (q != "" || a != "" || n!= "") {
        		QuestionAndAnswer qa = new QuestionAndAnswer(q, Integer.parseInt(a));
        		quiz.add(qa);
        		quiz.setName(n);
        		MainActivity.quizList.add(quiz);
        		quiz = null;
        		qNr = 1;
        		
        		FragmentManager fragmentManager = getFragmentManager();
	            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

	            MenuFragment newMenu = new MenuFragment();
	            fragmentTransaction.replace(R.id.fragment_container, newMenu);
	            fragmentTransaction.addToBackStack(null);
	            fragmentTransaction.commit();
    		}
    		else {
    			EditText etQuestion = (EditText) aView.findViewById(R.id.et_question);
        		etQuestion.setHint("Enter question here");
        		EditText etAnswer = (EditText) aView.findViewById(R.id.et_answer);
        		etAnswer.setHint("Enter answer here");
        		EditText etName = (EditText) aView.findViewById(R.id.et_quiz_name);
        		etName.setHint("Enter quiz name here");
    		}
    	}
	}

}
